<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <meta content="IE=edge" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1, shrink-to-fit=no" name="viewport">
    <meta content="featon inc." name="author">
    <link href="/static/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="/static/vendor/bootstrap/css/bootstrap-theme.min.css" rel="stylesheet">
    <link href="/static/vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <link href="/static/vendor/skin/ftskin.css" rel="stylesheet">
    <link href="/static/vendor/webuploader/webuploader.css" rel="stylesheet" type="text/css">
    <link href="/static/vendor/datetimepicker/jquery.datetimepicker.css" rel="stylesheet">
    <title>添加(更新企业)</title>
</head>

<body style="margin-bottom: 60px;">

<div class="ft-subpage-wrapper container-fluid">
    <form action="#" id="form1" method="post" name="form1">
        <div class="row">
            <div class="col-lg-12" id="form_table">
                <fieldset>
                    <legend id="titleText">-添加(更新企业)-</legend>
                    <table cellpadding="0" cellspacing="0" class="form-table form-table-border" id="">
                        <tr>
                            <th><span class="f_red">*</span>企业名称</th>
                            <td colspan="2">
                                <input class="form-control" id="name" name="name" placeholder="" type="text"
                                       value="">
                                <span class="cError"></span>
                            </td>
                        </tr>
                        <tr>
                            <th><span class="f_red">*</span>相关证件</th>
                            <td>
                                <select class="form-control" id="zjType" name="zjType">
                                    <option disabled selected>--请选择--</option>
                                    <option>营业执照</option>
                                    <option>身份证</option>
                                    <option>其他证件</option>
                                </select>
                                <span class="cError"></span>
                            </td>
                            <td>
                                <input class="form-control" id="yyCode" name="yyCode" placeholder="证件号码" type="text"
                                       value="">
                                <span class="cError"></span>
                            </td>
                        </tr>
                        <tr>
                            <th><span class="f_red">*</span>企业类型</th>
                            <td colspan="2">
                                <select class="form-control" id="companyType" name="companyType"
                                        style="width:300px">
                                    <option id="companyType_val" disabled selected>--请选择--</option>
                                </select>
                                <span class="cError"></span>
                            </td>
                        </tr>

                        <tr>
                            <th><span class="f_red">*</span>法人（负责人）</th>
                            <td colspan="2">
                                <input class="form-control" id="legalMan" name="legalMan" placeholder="法人（负责人）姓名"
                                       type="text">
                                <span class="cError"></span>
                            </td>
                        </tr>
                        <tr>
                            <th><span class="f_red">*</span>联系电话</th>
                            <td colspan="2">
                                <input class="form-control" id="phone" name="phone" placeholder="联系电话" type="tel">
                                <span class="cError"></span>
                            </td>
                        </tr>

                        <tr>
                            <th>
                                <span class="f_red">*</span>区县
                            </th>
                            <td>
                                <select class="form-control dq" id="dq3Id" name="dq3Id">
                                    <option disabled selected id="dq3Id_val">--请选择--</option>
                                </select>
                                <span class="cError"></span>
                            </td>
                            <td>
                                <select class="form-control dq" id="dq4Id" name="dq4Id">
                                    <option disabled selected id="dq4Id_val">--请选择--</option>
                                </select>
                                <span class="cError"></span>
                            </td>
                        </tr>

                        <tr>
                            <th><span class="f_red">*</span>详细地址</th>
                            <td colspan="2">
                                <input class="form-control" id="address" name="address" placeholder="详细地址" type="text">
                                <span class="cError"></span>
                            </td>
                        </tr>
                        <tr>
                            <th><span class="f_red">*</span>经纬度坐标</th>
                            <td>
                                <input class="form-control coordinate" id="longitude" name="longitude"  placeholder="" type="text"
                                       value="">
                                <span class="cError"></span>
                            </td>
                            <td>
                                <input class="form-control coordinate" id="dimensions" name="dimensions"  placeholder="" type="text"
                                       value="">
                                <span class="cError"></span>
                            </td>
                        </tr>
                        <tr>
                            <th>备注</th>
                            <td colspan="2">
                                <textarea class="form-control" id="remark" name="remark" rows="2"></textarea>
                            </td>
                        </tr>

                    </table>
                </fieldset>
                <div class="bottom-tools-box">
                    <button class="btn btn-info" id="save_but" onclick="addAndUpdateObject()" type="button"><span
                            aria-hidden="true" class="glyphicon glyphicon-floppy-disk"></span> 保存
                    </button>
                    <button class="btn btn-warning" onclick="top.tab_colseOnclick();" type="button"><span
                            aria-hidden="true" class="glyphicon glyphicon-remove-circle"></span> 取消
                    </button>
                </div>

            </div>
        </div>
        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
            <div class="modal-dialog modal-lg" role="document">
                <div class="modal-content">
                    <div id="r-result" style="margin-left: 20px">
                        地址: <input id="cityName" type="text" style="width:300px; margin:10px;" />
                        <input type="button" value="搜索" onclick="theLocation()" />
                    </div>
                    <!-- 地图盒子 -->
                    <div id="allmap" style="height:600px"></div>
                </div>
            </div>
        </div>

    </form>
</div>
<input th:id="qyId" th:value="${qyId}" type="hidden"/>
<script src="/static/vendor/jquery/jquery-3.3.1.min.js"></script>
<script src="/static/vendor/bootstrap/js/bootstrap.min.js"></script>
<script src="/static/vendor/validate/jquery.validate.min.js" type="text/javascript"></script>
<script src="/static/vendor/webuploader/webuploader.js" type="text/javascript"></script>
<script src="/static/vendor/skin/js/ft-ui.js"></script>
<script src="/static/vendor/skin/js/jquery.c.js"></script>
<script src="/static/vendor/skin/js/java.js"></script>
<script src="/static/vendor/skin/js/upload-util.js"></script>
<script src="/static/vendor/sortable/sortable.js"></script>
<script src="/static/vendor/datetimepicker/jquery.datetimepicker.js"></script>
<script type="text/javascript" src="https://api.map.baidu.com/api?key=&v=1.3&services=true"></script>
<script>
    $("form input,form select").blur(function () {
        let siblings = $(this).siblings(".cError");
        $(siblings).empty();
        if ($(this).val().trim() === "" || $(this).val() === null) {
            $(siblings).append(`
            <span style="font-size: 12px;font-weight: bold;color: red">不能为空</span>
            `)
            $(this).focus();
        }
    })


    let $qyid = $("#qyId").val();
    $(function () {
        $("#name").focus()
        if ($qyid !== null && $qyid !== '') {
            $.post("/ci/getCompanyByIdOrNameOrCode?qyId=" + $qyid, function (result) {
                console.log(result)
                $("#name").val(result.name);
                $("#zjType").val(result.zjType);
                $("#yyCode").val(result.yyCode);
                $("#companyType_val").text(result.companyType);
                $("#legalMan").val(result.legalMan);
                $("#phone").val(result.phone);
                $("#dq3Id_val").text(result.dq3Id);
                $("#dq4Id_val").text(result.dq4Id);
                $("#address").val(result.address);
                $("#dimensions").val(result[0].dimensions);
                $("#remark").val(result.remark);
            })
        }
    })

    function iniInfoForm() {
        $("#form1").validate({
            rules: {
                name: {
                    required: true
                },
                zjType: {
                    required: true
                },
                yyCode: {
                    required: true
                },
                companyType: {
                    required: true
                },
                legalMan: {
                    required: true
                },
                phone: {
                    required: true
                },
                dq3Id: {
                    required: true
                },
                dq4Id: {
                    required: true
                },
                address: {
                    required: true
                }
            },
            messages: {
                name: {
                    required: "不能为空！"
                },
                zjType: {
                    required: "不能为空！"
                },
                yyCode: {
                    required: "不能为空！"
                },
                companyType: {
                    required: "不能为空！"
                },
                legalMan: {
                    required: "不能为空！"
                },
                phone: {
                    required: "不能为空！"
                },
                dq3Id: {
                    required: "不能为空！"
                },
                dq4Id: {
                    required: "不能为空！"
                },
                address: {
                    required: "不能为空！"
                }
            },
            errorPlacement: function (error, element) {
                error.appendTo(element.parents('td').find(".cError"));
            },
            submitHandler: function (form) {
            }
        });
    }

    let url = '';
    if ($qyid === null || $qyid === '') {
        url = '/ci/addCompany';
    } else {
        url = '/ci/updateCompany';
    }

    function addAndUpdateObject() {

        let bean = {
            qyId: $("#qyId").val(),
            name: $("#name").val(),
            zjType: $("#zjType").val(),
            yyCode: $("#yyCode").val(),
            companyType: $("#companyType").val(),
            legalMan: $("#legalMan").val(),
            phone: $("#phone").val(),
            dq3Id: $("#dq3Id").val(),
            dq4Id: $("#dq4Id").val(),
            address: $("#address").val(),
            dimensions: $("#dimensions").val(),
            remark: $("#remark").val()
        }
        let submit = true;
        if ($qyid === null || $qyid === ''){
            for (let beanKey in bean) {
                let beanElement = bean[beanKey];
                if ((beanElement === null || beanElement === "") && beanKey !== "qyId" && beanKey !== "remark") {
                    submit = false;
                    $("#" + beanKey).siblings(".cError").append(`<span style="font-size: 12px;font-weight: bold;color: red">不能为空</span>`);
                    return;
                }
            }
        }
        if (submit) {
            if (top.ajax.synclBody(url, bean)) {
                modelWindow.alert(top.CMSLang.Add_success);
                top.getCurrentFrameObj().loadListble();
                top.tab_colseOnclick();
            } else {
                modelWindow.alert(top.CMSLang.Add_fail);
            }
        }
    }
    function f($that) {
        let thisId = $($that).attr('id');
        let area = '610300000000';
        let $dq3Id = $("#dq3Id");
        if (thisId === 'dq4Id') {
            $("#dq4Id_val").hide();
            let subArea = $dq3Id.val()
            if (subArea !== '' || subArea !== null) {
                area = subArea;
            }
        } else {
            $("#dq3Id_val").hide();
            $("#dq4Id").empty().append(` <option disabled selected>--请选择--</option>`);
        }
        let url = '/diqu/getChildDiquList/' + area;
        let children = $($that).children();
        if (children.length <= 1) {
            $.get(url, function (result) {
                result.forEach((item) => {
                    if (item.dq_name ===$("#dq3Id_val").text()){
                        $($that).append(`<option selected  value=` + item.dq_id + `>` + item.dq_name + `</option>`)
                    }else {
                        $($that).append(`<option  value=` + item.dq_id + `>` + item.dq_name + `</option>`)
                    }
                })
            })
        }
    }
    $("#dq3Id").click(function () {
         $that = $(this);
        f($that);
    })

    $("#dq4Id").click(function () {
        let areaName='';
        let url='';
        if ($('#dq3Id').val()!==null){
            areaName =  $('#dq3Id').val()
            url = '/diqu/getChildDiquList/' + areaName;
        }else {
            areaName =  $('#dq3Id_val').val()
             url = '/ci/getChildDiquListByDq3Name/' + areaName;
        }
        $that = $(this);

        let children = $($that).children();
        if (children.length <= 1) {
            $.get(url, function (result) {
                result.forEach((item) => {
                    if (item.dq_name ===$("#dq4Id_val").text()){
                        $($that).append(`<option selected  value=` + item.dq_id + `>` + item.dq_name + `</option>`)
                    }else {
                        $($that).append(`<option  value=` + item.dq_id + `>` + item.dq_name + `</option>`)
                    }
                })
            })
        }
    })




    getCompany_type()
    function getCompany_type() {
        let $that = $("#companyType");
        let url = '/dictCategory/getDictList/company_type';
        let children = $($that).children();
        if (children.length <= 1) {
            $("#companyType_val").hide();
            $.get(url, function (result) {
                result.forEach((item) => {
                    if (item.dict_name === $("#companyType_val").text()){
                        $($that).append(`<option selected value="` + item.dict_code + `">` + item.dict_name + `</option>`)
                    }else {
                        $($that).append(`<option value="` + item.dict_code + `">` + item.dict_name + `</option>`)
                    }
                })
            })
        }
    }
    $(".coordinate").focus(function () {
        $('#myModal').modal('show');
        $('#cityName').val($('#address').val())
        theLocation();
    })

    // 在指定容器创建地图实例并设置最大最小缩放级别
    var map = new BMap.Map("allmap", {
        minZoom: 5,
        maxZoom: 19
    });


    // 初始化地图，设置中心点和显示级别
    map.centerAndZoom(new BMap.Point(107.276394, 34.335655), 15);

    // 开启鼠标滚轮缩放功能，仅对PC上有效
    map.enableScrollWheelZoom(true);

    // 将控件（平移缩放控件）添加到地图上
    map.addControl(new BMap.NavigationControl());
    // 创建位置检索、周边检索和范围检索
    var local = new BMap.LocalSearch(map, {
        renderOptions: {
            map: map
        }
    });

    // 发起检索
    function theLocation() {
        var city = document.getElementById("cityName").value;
        if (city != "") {
            local.search(city);
        }
    };
    // 为地图增加点击事件，为input赋值
    var marker
    map.addEventListener("click", function(e) {
        document.getElementById('dimensions').value = e.point.lat;
        document.getElementById('longitude').value = e.point.lng;
        var point = new BMap.Point(e.point.lng,e.point.lat); //将标注点转化成地图上的点
        map.removeOverlay(marker);
        marker = new BMap.Marker(point); //将点转化成标注点
        map.addOverlay(marker);  //将标注点添加到地图上

    });
</script>
</body>

</html>
